<?php

namespace Home\Controller;

use Think\Controller;

use Think\Model;

use Think\Log;

use PublicClass1_0_0;
//处理跨域  (处理接收，不能删)
header("Access-Control-Allow-Origin:*"); //*号表示所有域名都可以访问  
header("Access-Control-Allow-Method:POST,GET");
date_default_timezone_set('Asia/Shanghai');
/**
 * 接口类 
 */
class ApiController extends Controller {
    
    //接口首页
    public function index() {
        echo '<center><span style="color:red;font-size:25px;">欢迎来到前海接口首页,请输入正确接口地址！！！</span></center>';      
    }  
    
    //点击量（goods表）
    public function click() {
        $goods = M('goods');
        $goods_id = $_REQUEST['goods_id'];
        if (!empty($goods_id)) {
            $where['id'] = $goods_id; 
            $where['status'] = '1';
            $res = $goods->where($where)->setInc('click',1);
            if ($res) {
                $code = array('status_code'=>'1');
                echo arr_json($code);
            } else {
                $code = array('status_code'=>'3','result_msg'=>'点击量增加失败');
                echo arr_json($code);
            }
           
        } else {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
        }    
    }
    
    //发送短信验证码
    public function send(){
        $user = M('user');
        $mobile = $_REQUEST['phone'];
        //$mobile = '13590263965';
        $token = mt_rand('1000','9999');
        $res = $user->where(array('phone'=>$mobile,'status'=>'1'))->select();
        if ($res) {
            //手机号存在，验证码存进数据库
            $user->token = $token;
            $user->where(array('phone'=>$mobile))->save();
        }
        if (empty($mobile)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        session_start();
        $_SESSION['user_info'] = $mobile.'-'.$token;
        $post_data = array();
        $post_data['account'] = '522pg1';
        $post_data['pswd'] = 'o32GiXRw';
        $post_data['needstatus'] = 'false';
        $post_data['msg'] = '您的验证码为:'.$token; 
        $post_data['mobile'] = $mobile;
        $post_data['product'] = ''; 
        $post_data['extno'] = '';
        $url = 'http://send.18sms.com/msg/HttpBatchSendSM?';
        /* 用&拼接字符串,作用和函数http_build_query一样，如果是中文会进行转码 */
        $o = '';
        foreach ($post_data as $k=>$v) {
            $o .= $k.'='.urlencode($v).'&';
        }
        //去掉最后一个字符，这里为&
        $data = substr($o,0,-1);     
        //创建一个新curl资源
        $ch = curl_init();
        //启动时发送一个常规的POST请求，就像表单提交的一样
        curl_setopt($ch,CURLOPT_POST,1);
        //启动时会将头文件的信息作为数据流输出
        curl_setopt($ch,CURLOPT_HEADER,0);
        //需要获取的URL地址
        curl_setopt($ch,CURLOPT_URL,$url);
        //尽量对POST提交的数据进行处理，格式为（这里的格式与CURLOPT_POST对应）
        //userid=692&account=qizhi&password=123456&content=您的验证码为:5682&mobile=13590263965&sendTime=&action=send&extno=
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
        //将curl_exec()获取的信息以文件流的形式返回即将结果直接返回到变量里
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        $result = curl_exec($ch);
        $date = date('Y-m-d H;i;s',time());
        $code = array('status_code'=>'1','phone'=>$mobile,'code'=>$token,'date'=>$date);
        echo arr_json($code);
    }
    
     //用户注册
    public function register() {
        $user = M('user');
        //$phone = '13590263965';
        $phone = $_REQUEST['phone'];
        //$code = '3734';
        $code = $_REQUEST['code'];
        //$password = '225625';
        $password = $_REQUEST['password'];
        session_start();
        $arr = explode('-',$_SESSION['user_info']);
        $mobile = $arr['0'];
        $token = $arr['1'];
        if (empty($phone) || empty($code) || empty($password)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $res = $user->where(array('phone'=>$phone,'status'=>'1'))->select();
        if ($res) {
            $code = array('status_code'=>'3','result_msg'=>'该手机号已注册');
            echo arr_json($code);
            exit;
        }
        if ($phone == $mobile && $code != $token) {
            $code = array('status_code'=>'4','result_msg'=>'验证码错误或失效');
            echo arr_json($code);
            exit;
        }
        $data['password'] = md5($password);
        $data['phone'] = $phone;
        $data['token'] = $code;
        $data['nickname'] = '车网通'.mt_rand('10000','99999');
        $data['head_img'] = '/uploads/image/20160816/setting-img.png';
        $data['age'] = '90后';
        $data['reg_time'] = time();
        $list = $user->add($data);
        if (!$list) {
            $code = array('status_code'=>'5','result_msg'=>'注册失败');
            echo arr_json($code);
            exit;
        }
        //生成个人消息中心
        M('sys_msg')->add(array('title'=>'个人消息','content'=>'恭喜您成为车网通用户','status'=>'1','create_time'=>time(),'type'=>'1','user_id'=>$list));
        //更新登录地址ip和时间
        $user->last_login_time = time();
        $user->last_login_ip = $_SERVER["REMOTE_ADDR"];
        $user->where(array('phone'=>$phone))->save();
        $res2 = $user->field('id,phone,token,last_login_time,last_login_ip')->where(array('phone'=>$phone,'token'=>$code,'status'=>'1'))->select();
        //时间格式化
        foreach ($res2 as $k=>$v) {             
            $res2[$k]['last_login_date'] = date('Y-m-d H:i:s',$v['last_login_time']);
            unset($res2[$k]['last_login_time']);
        }
        $returnArray = array('status_code'=>'1','user_info'=>$res2);
        $this ->ajaxReturn($returnArray);
    }
    
    //用户登录
    public function login() {
        $user = M('user');
        $phone = $_REQUEST['phone'];
        //$phone = '13590263965';
        $code = $_REQUEST['code'];
        //$code = '2354';
        $pwd = $_REQUEST['password'];
        //$pwd = '123456';
        $password = md5($pwd);
        if (empty($phone)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $res = $user->where(array('phone'=>$phone,'status'=>'1'))->select();
        if (!$res) {
            $code = array('status_code'=>'3','result_msg'=>'该手机号还未注册');
            echo arr_json($code);
            exit;
        }
        if (!empty($code) && !empty($pwd)) {
            $code = array('status_code'=>'4','result_msg'=>'传参多余');
            echo arr_json($code);
            exit;
        }
        if (empty($code) && empty($pwd)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        if (!empty($code) && empty($pwd)) {//手机+验证码登录
            session_start();
            $arr = explode('-',$_SESSION['user_info']);
            $mobile = $arr['0'];
            $token = $arr['1'];
            $user->token = $token;
            $user->where(array('phone'=>$mobile))->save();
            $res1 = $user->field('id,phone,token')->where(array('phone'=>$phone,'token'=>$code,'status'=>'1'))->select();
            if (!$res1) {
                $code = array('status_code'=>'5','result_msg'=>'验证码错误或失效');
                echo arr_json($code);
                exit;
            }
            //更新登录地址ip和时间
            $user->last_login_time = time();
            $user->last_login_ip = $_SERVER["REMOTE_ADDR"];
            $user->where(array('phone'=>$phone))->save();
            $res2 = $user->field('id,phone,token,last_login_time,last_login_ip')->where(array('phone'=>$phone,'token'=>$code,'status'=>'1'))->select();
            //时间格式化
            foreach ($res2 as $k=>$v) {             
                $res2[$k]['last_login_date'] = date('Y-m-d H:i:s',$v['last_login_time']);
                unset($res2[$k]['last_login_time']);
            }
            $code = array('status_code'=>'1','user_info'=>$res2);
            echo arr_json($code);
        }
        if (!empty($pwd) && empty($code)) {//手机+密码登录            
            $res1 = $user->field('id,phone,password')->where(array('phone'=>$phone,'password'=>$password,'status'=>'1'))->select();
            if (!$res1) {
                $code = array('status_code'=>'6','result_msg'=>'密码错误');
                echo arr_json($code);
                exit;
            }
            //更新登录地址ip和时间
            $user->last_login_time = time();
            $user->last_login_ip = $_SERVER["REMOTE_ADDR"];
            $user->where(array('phone'=>$phone))->save();
            $res2 = $user->field('id,phone,password,last_login_time,last_login_ip')->where(array('phone'=>$phone,'password'=>$password,'status'=>'1'))->select();
            //把时间格式化
            foreach ($res2 as $k=>$v) {             
                $res2[$k]['last_login_date'] = date('Y-m-d H:i:s',$v['last_login_time']);
                unset($res2[$k]['last_login_time']);
            }
            $code = array('status_code'=>'1','user_info'=>$res2);
            echo arr_json($code);
        }
    }
     
    //忘记密码
    public function forgetPwd() {
        $user = M('user');
        $phone = $_REQUEST['phone'];
        //$phone = '13590263965';
        $code = $_REQUEST['code'];
        //$code = '2354';
        session_start();
        $arr = explode('-',$_SESSION['user_info']);
        $mobile = $arr['0'];
        $token = $arr['1'];
        $user->token = $token;
        $user->where(array('phone'=>$phone))->save();
        if (empty($phone) || empty($code)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        } 
        $res = $user->where(array('phone'=>$phone,'status'=>'1'))->select();
        if (!$res) {
            $code = array('status_code'=>'3','result_msg'=>'该手机号还未注册');
            echo arr_json($code);
            exit;
        }
        if ($phone == $mobile && $code != $token) {
            $code = array('status_code'=>'4','result_msg'=>'验证码错误或失效');
            echo arr_json($code);
            exit;
        }
        /*-----↑手机验证通过↑------*/
        $code = array('status_code'=>'1');
        echo arr_json($code);
    }
    
    //重置密码
    public function resetPwd() {
        $user = M('user');
        $phone = $_REQUEST['phone'];
        //$phone = '13590263965';
        $n_password = $_REQUEST['n_password'];
        //$n_password = '123456';
        if (empty($phone) || empty($n_password)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $user->password = md5($n_password);
        $list = $user->where(array('phone'=>$phone))->save();
        $code = array('status_code'=>'1');
        echo arr_json($code);
    }
    
    //我的收藏（查询）
    public function collectSel() {
        $user_id = $_REQUEST['user_id'];
        if (empty($user_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $res = M('collect')->field('id,type,type_id')->where(array('user_id'=>$user_id))->order('type asc,add_time desc')->select();
        if (!$res) {
            $code = array('status_code'=>'3','result_msg'=>'暂无收藏');
            echo arr_json($code);
            exit;
        }
        foreach ($res as $k=>$v) {
            if ($res[$k]['type'] == 1  || $res[$k]['type'] == 4) {
                
                $res1 = M('goods as a')->field('a.id as goods_id,a.thumb,a.name,a.label,a.price')->join('eb_collect as b on a.id=b.type_id')->where(array('a.id'=>$v['type_id']))->find();                      
                $res1['thumb'] = C('HTTP_HEAD').$res1['thumb'];
            }
            if ($res[$k]['type'] == 2) {
                $res1 = M('notice as a')->field('a.id as notice_id,a.thumb,a.title,a.add_time')->join('eb_collect as b on a.id=b.type_id')->where(array('a.id'=>$v['type_id'],'a.hide'=>'0'))->find();
                $res1['thumb'] = C('HTTP_HEAD').$res1['thumb'];
                $res1['add_time'] = date('Y-m-d H:i:s',$res1['add_time']);
            }
            if ($res[$k]['type'] == 3) {
                $res1 = M('activity as a')->field('a.id as activity_id,a.name,a.thumb,a.url')->join('eb_collect as b on a.id=b.type_id')->where(array('a.id'=>$v['type_id'],'a.status'=>'1'))->find();
                $res1['thumb'] = C('HTTP_HEAD').$res1['thumb'];
            }
            if ($res[$k]['type'] == 5) {
                $groupBuy = D('GroupBuy');
                $res1 = $groupBuy -> isCollect($v['type_id']);
                
            }
            $res[$k]['collect_info'] = $res1;                                  
        }        
        $code = array('status_code'=>'1','result_msg'=>$res);
        echo arr_json($code);                              
    }
    
    //我的收藏（添加）
    public function collectAdd() {
        $type = $_REQUEST['type'];
        $type_id = $_REQUEST['type_id'];
        $user_id = $_REQUEST['user_id'];
        if (empty($type) || empty($type_id) || empty($user_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $res = M('collect')->fetchSql(false)->where(array('type'=>$type,'type_id'=>$type_id,'user_id'=>$user_id))->select();
        
        if (!empty($res)) {
            $code = array('status_code'=>'3','result_msg'=>'已收藏');
            echo arr_json($code);
            exit;
        }
        M('collect')->fetchSql(false)->add(array('type_id'=>$type_id,'type'=>$type,'user_id'=>$user_id,'add_time'=>time()));
        $code = array('status_code'=>'1');
        echo arr_json($code);
    }
    
    //我的收藏（删除）
    public function collectDel() {
        $type = $_REQUEST['type'];
        $type_id = $_REQUEST['type_id'];
        $user_id = $_REQUEST['user_id'];
        if (empty($type) || empty($type_id) || empty($user_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        M('collect')->where(array('type'=>$type,'type_id'=>$type_id,'user_id'=>$user_id))->delete();
        $code = array('status_code'=>'1');
        echo arr_json($code);
    }
    
    //用户信息列表getUserInfoList
    public function selUserInfo() {
        $user = M('user');
        $user_id = $_REQUEST['user_id'];
        if (empty($user_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
        } else {
            $res = $user->field('head_img,nickname,phone,sex,age')->where(array('id'=>$user_id,'status'=>'1'))->select();
            foreach($res as $k=>$v) {
                $res[$k]['head_img'] = C('HTTP_HEAD').$v['head_img'];
            }
            $code = array('status_code'=>'1','user_info'=>$res);
            echo arr_json($code);
        }
    }
    
    //设置用户信息
    public function setUserInfo() {
        $user = M('user');
        $user_id = $_REQUEST['user_id'];
        //$user_id = '2374';
        if(!empty($_FILES)) {
            $upl = $this->upload();
            $head_img = '/uploads'.trim($upl['upfile']['savepath'],'.').$upl['upfile']['savename'];
        }
        $nickname = $_REQUEST['nickname'];
        $sex = $_REQUEST['sex'];
        $age = $_REQUEST['age'];
        //$o_password = '123456';
        $o_password = $_REQUEST['o_password'];
        $n_password = $_REQUEST['n_password'];
        //$n_password = '111111';
        if (empty($user_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        } else {
            if (!empty($head_img)) {
                $user->head_img = $head_img;
            }
            if (!empty($nickname)) {
                $user->nickname = $nickname;
            }
            if (!empty($sex)) {
                if ($sex == '男') {
                    $user->sex = 1;
                } else if ($sex == '女') {
                    $user->sex = 2;
                } else {
                    $user->sex = 0;
                }
            }
            if (!empty($age)) {
                $user->age = $age;
            }
            if (!empty($o_password) && !empty($n_password)) {
                $res = $user->where(array('id'=>$user_id,'status'=>'1'))->select();
                if ($res['0']['password'] == md5($o_password)) {
                    $user->password = md5($n_password);
                } else {
                    $code = array('status_code'=>'3','result_msg'=>'原密码不正确');
                    echo arr_json($code);
                    exit;
                }
            }     
            $user->where('id='.$user_id)->save();
            $res = $user->field('id,password,head_img,nickname,sex,age')->where(array('id'=>$user_id,'status'=>'1'))->select();
            foreach($res as $k=>$v) {
                $res[$k]['head_img'] = C('HTTP_HEAD').$v['head_img'];
            }
            $code = array('status_code'=>'1','user_info'=>$res);
            echo arr_json($code);            
        }
    }
    
    //意见反馈（添加）
    public function feedback() {
        $feedback = M('feedback');
        $content = $_REQUEST['content'];
        //$phone = '13895948561';
        $phone = $_REQUEST['phone'];
        $email = $_REUQEST['email'];
        if (empty($content)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        if (!empty($phone)) {
            $data['phone'] = $phone;
        }
        if (!empty($email)) {
            $data['email'] = $email;
        }
        $data['content'] = $content;
        $data['status'] = '0';
        $data['add_time'] = time();
        $list = $feedback->add($data);
        if (!$list) {
            $code = array('status_code'=>'3','result_msg'=>'添加失败');
            echo arr_json($code);
            exit;
        }
        $code = array('status_code'=>'1');
        echo arr_json($code);
    }
    
    //消息中心（查询）
    public function sysMsgSel() {
        $user_id = $_REQUEST['user_id'];
        if (empty($user_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $res = M('sys_msg_del')->where(array('user_id'=>$user_id))->select();
        if ($res) {
            foreach ($res as $k=>$v) {
                $id .= $v['msg_id'].',';
            }
            $id = substr($id,0,-1);//删除最后一个逗号
        } else {
            $id = '0';
        }
        $res1 = M('sys_msg')->where(array('status'=>'1','user_id'=>array('in',array('0',$user_id)),'id'=>array('not in',$id)))->order('create_time desc')->select();
        if (!$res1) {
            $code = array('status_code'=>'3','result_msg'=>'暂无消息');
            echo arr_json($code);
            exit;
        }
        $code = array('status_code'=>'1','sys_info'=>$res1);
        echo arr_json($code);
    }
    
    //消息中心（删除）
    public function sysMsgDel() {
        $user_id = $_REQUEST['user_id'];
        $sys_msg_id = $_REQUEST['sys_msg_id'];
        $type = $_REQUEST['type'];
        if (empty($user_id) || empty($sys_msg_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        if ($type == '0') {//系统消息
            $res = M('sys_msg_del')->where(array('user_id'=>$user_id,'msg_id'=>$sys_msg_id))->select();
            if (!$res) {
                $list = M('sys_msg_del')->add(array('user_id'=>$user_id,'msg_id'=>$sys_msg_id));
                if (!$list) {
                    $code = array('status_code'=>'4','result_msg'=>'添加失败');
                    echo arr_json($code);
                    exit;
                }
            }     
        } else {
            $list = M('sys_msg')->where(array('user_id'=>$user_id,'id'=>$sys_msg_id))->save(array('status'=>'0'));
            if (!$list) {
                $code = array('status_code'=>'3','result_msg'=>'删除失败');
                echo arr_json($code);
                exit;
            }
        }
        $code = array('status_code'=>'1');
        echo arr_json($code);
    }   
    
     //我的车辆（添加）
    public function myCarAdd() {
        $car = M('car');
        //$user_id = '2251';
        $user_id = $_REQUEST['user_id'];
        //$goods_id = '282';
        $goods_id = $_REQUEST['goods_id'];
        //$vehicle = '粤B306QA';
        $vehicle = $_REQUEST['vehicle'];
        //$frame_no = '018342';
        $frame_no = $_REQUEST['frame_no'];
        //$engine_no = '018342';
        $engine_no = $_REQUEST['engine_no'];
        //$run_mileage = '3000';
        $run_mileage = $_REQUEST['run_mileage'];
        if (empty($user_id) || empty($goods_id) || empty($vehicle) || empty($frame_no) || empty($engine_no) || empty($run_mileage)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $res = $car->where(array('user_id'=>$user_id,'vehicle'=>$vehicle))->select();
        if ($res) {
            $code = array('status_code'=>'3','result_msg'=>'当前用户下已存在相同车牌号码的车辆');
            echo arr_json($code);
            exit;
        }
        $goods_res = $car->table(C('DB_PREFIX').'goods')->where(array('id'=>$goods_id,'status'=>1,'goods_type'=>0))->select();
        $cate_id = $goods_res['0']['cate_id'];
        $name = $goods_res['0']['name'];
        $goods_cate_res = $car->table(C('DB_PREFIX').'goods_cate')->where(array('id'=>$cate_id))->select();
        $thumb = $goods_cate_res['0']['thumb'];
        //插入数组
        $data['name'] = $name;
        $data['thumb'] = $thumb;
        $data['vehicle'] = $vehicle;
        $data['frame_no'] = $frame_no;
        $data['engine_no'] = $engine_no;
        $data['run_mileage'] = $run_mileage;
        $data['user_id'] = $user_id;
        $list = $car->add($data);
        if (!$list) {
            $code = array('status_code'=>'10','result_msg'=>'第,'.$list.'插入数据失败');
            echo arr_json($code);
            exit;
        }
        $code = array('status_code'=>'1');
        echo arr_json($code);
    }
     
    //我的车辆（查询）
    public function myCarSel() {
        $car = M('car');
        //$user_id = 2250;
        $user_id = $_REQUEST['user_id'];
        if (empty($user_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $res = $car->where(array('user_id'=>$user_id))->select();
        if (!$res) {
            $code = array('status_code'=>'3','result_msg'=>'该用户还未添加车辆');
            echo arr_json($code);
            exit;
        }
        foreach ($res as $k=>$v) {
            $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
        }
        $code = array('status_code'=>'1','my_cars'=>$res);
        echo arr_json($code);
    }
    
    //我的车辆（编辑）
    public function myCarSave() {
        $car = M('car');
        //$user_id = '2250';
        $user_id = $_REQUEST['user_id'];
        //$car_id = '4';
        $car_id = $_REQUEST['car_id'];
        //$name = '2016款丰田塞纳L版3.5L';
        $name = $_REQUEST['name'];
        //$vehicle = '粤B304QA';
        $vehicle = $_REQUEST['vehicle'];
        //$frame_no = '018340';
        $frame_no = $_REQUEST['frame_no'];
        //$engine_no = '018340';
        $engine_no = $_REQUEST['engine_no'];
        //$run_mileage = '3000';
        $run_mileage = $_REQUEST['run_mileage'];
        if (empty($user_id) || empty($car_id) || empty($name) || empty($vehicle) || empty($frame_no) || empty($engine_no) || empty($run_mileage)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $ress = M('goods')->where(array('name'=>$name,'status'=>1,'goods_type'=>0))->select();
        $cate_id = $ress['0']['cate_id'];
        $ress1 = M('goods_cate')->where(array('id'=>$cate_id))->select();
        $thumb = $ress1['0']['thumb'];
        $car->name = $name;
        $car->thumb = $thumb;
        $car->vehicle = $vehicle;
        $car->frame_no = $frame_no;
        $car->engine_no = $engine_no;
        $car->run_mileage = $run_mileage;
        $list = $car->where(array('id'=>$car_id))->save();
        if (!$list) {
            $code = array('status_code'=>'3','result_msg'=>'没做任何修改');
            echo arr_json($code);
            exit;
        }
        $code = array('status_code'=>'1');
        echo arr_json($code);
    }
    
    //我的车辆（删除）
    public function myCarDel() {
        $car = M('car');
        //$user_id = '2250';
        $user_id = $_REQUEST['user_id'];
        //$car_id = '3';
        $car_id = $_REQUEST['car_id'];
        if (empty($user_id) || empty($car_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $list = $car->where(array('id'=>$car_id))->delete();
        if (!$list) {
            $code = array('status_code'=>'3','result_msg'=>'该车辆不存在');
            echo arr_json($code);
            exit;
        }
        $code = array('status_code'=>'1');
        echo arr_json($code);
    }
    
    //前海广告图
    public function carousel() {
    
        $mcarousel = M('mcarousel');
        
        $res = $mcarousel->where(array('status'=>'1','type'=>'1'))->order('sort desc')->select();

        foreach($res as $k=>$v) {

            $res[$k]['pic'] = C('HTTP_HEAD').$v['pic'];

        }
       
        $code = array('status_code'=>'1','picture'=>$res);
                
        echo arr_json($code);
        
    }
    
    //车生活轮播图
    public function carlife_carousel() {
        
        $mcarousel = M('mcarousel');
        
        $res = $mcarousel->where(array('status'=>'1','type'=>'2'))->order('sort desc')->select();

        foreach($res as $k=>$v) {

            $res[$k]['pic'] = C('HTTP_HEAD').$v['pic'];
            
        }
        
        $code = array('status_code'=>'1','car_life_pic'=>$res);
        
        echo arr_json($code);
        
    }
    
    //前海头条
    public function news() {
        
        $notice = M('notice');
        
        $res = $notice->where('hide=0')->order('add_time desc')->select();

        foreach($res as $k=>$v) {

            $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
            $res[$k]['add_date'] = date('Y-m-d H:i:s',$v['add_time']);
            unset($res[$k]['add_time']);
        }
        $code = array('status_code'=>'1','news'=>$res);
        
        echo arr_json($code);
        
    }
    
    //资讯详情
    public function newsDetail() {
        $notice_id = $_REQUEST['notice_id'];
        $user_id = $_REQUEST['user_id'];//非必传参数，用来判断是否已收藏
        //判断是否收藏
        if (!empty($user_id)) {
            $ress = M('user')->where(array('id'=>$user_id))->select();
            $collect_id = $ress['0']['collect_id'];
            $ress1 = M('collect')->where(array('id'=>array('in',$collect_id)))->select();
            foreach ($ress1 as $k1=>$v1) {
                $str_collect .= $v1['type_id'].',';
            }
            $new_str = substr($str_collect,0,-1);
            $new_arr = explode(',',$new_str);
            if (in_array($notice_id,$new_arr)) {
                $is_collect = '1';
            } else {
                $is_collect = '0';
            }
        } else {
            $is_collect = '0';
        }
        $res = M('notice')->field('title,add_time,thumb,content,author')->where(array('id'=>$notice_id,'hide'=>'0'))->select();
        if (empty($notice_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        foreach ($res as $k=>$v) {
            $res[$k]['add_date'] = date('Y-m-d H:i:s',$v['add_time']);
            $res[$k]['is_collect'] = $is_collect;
            unset($res[$k]['add_time']);
        }
         //微信分享
        $url = $_REQUEST['url'];
        $pay_config = D('PayConfig');
        $wx_config_data = $pay_config -> getOneList(array(pay_status => 0,use_status => 1), 'app_id,mch_id,key,app_secret');
        if(empty($wx_config_data)) {
            $jsSdk = new PublicClass1_0_0\WechatClass();
        } else {
            $jsSdk = new PublicClass1_0_0\WechatClass($wx_config_data['app_id'],$wx_config_data['mch_id'],$wx_config_data['key'],$wx_config_data['app_secret']);
        }

        $wx = $jsSdk ->getShareConfig($url);
        
        $code = array('status_code'=>'1','news_detail'=>$res,'wx' => $wx);
        echo arr_json($code);
    }
    
    //资讯推荐
    public function newsRecommend() {
        $notice_id = $_REQUEST['notice_id'];
        if (empty($notice_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $res = M('notice')->field('id,title,add_time,thumb,author')->where(array('id'=>array('not in',$notice_id),'hide'=>'0'))->order('add_time desc')->limit(5)->select();
        foreach ($res as $k=>$v) {
            $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
            $res[$k]['add_date'] = date('Y-m-d H:i:s',$v['add_time']);
            unset($res[$k]['add_time']);
        }
        $code = array('status_code'=>'1','news_recommend'=>$res);
        echo arr_json($code);
    }
    
    //品牌推荐
    public function brandRecommend() {
        $goods = M('goods as a');
        $goods_cate = M('goods_cate');
        //$carbody1 = '跑车';
        $carbody1 = $_REQUEST['carbody1'];
        //$carbody2 = 'SUV';
        $carbody2 = $_REQUEST['carbody2'];
        //$carbody3 = '两厢';
        $carbody3 = $_REQUEST['carbody3'];
        if (empty($carbody1) || empty($carbody2) || empty($carbody3)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $res1 = $goods->field('id,cate_id,name,price,thumb,status,click')->where(array('status'=>'1','carbody'=>$carbody1,'goods_type'=>0))->order('click desc')->limit(3)->select();
        $res2 = $goods->field('id,cate_id,name,price,thumb,status,click')->where(array('status'=>'1','carbody'=>$carbody2,'goods_type'=>0))->order('click desc')->limit(3)->select();
        $res3 = $goods->field('id,cate_id,name,price,thumb,status,click')->where(array('status'=>'1','carbody'=>$carbody3,'goods_type'=>0))->order('click desc')->limit(3)->select();        
        if (empty($res1) && empty($res2) && empty($res3)) {
            $code = array('status_code'=>'3','result_msg'=>'找不到符合条件的车型');
            echo arr_json($code);
            exit;
        }
        foreach ($res1 as $k1=>$v1) {
            $pid1 = $v1['cate_id'];
            $res4 = $goods_cate->where('id='.$pid1)->select();
            $res1[$k1]['thumb'] = C('HTTP_HEAD').$v1['thumb'];
            $res1[$k1]['brand_name'] = $res4['0']['name'];
        } 
        foreach ($res2 as $k2=>$v2) {
            $pid2 = $v2['cate_id'];
            $res5 = $goods_cate->where('id='.$pid2)->select();
            $res2[$k2]['thumb'] = C('HTTP_HEAD').$v2['thumb'];
            $res2[$k2]['brand_name'] = $res5['0']['name'];
        } 
        foreach ($res3 as $k3=>$v3) {
            $pid3 = $v3['cate_id'];
            $res6 = $goods_cate->where('id='.$pid3)->select();
            $res3[$k3]['thumb'] = C('HTTP_HEAD').$v3['thumb'];
            $res3[$k3]['brand_name'] = $res6['0']['name'];
        } 
        $res11 = $goods->where(array('carbody'=>$carbody1,'status'=>1,'goods_type'=>0))->select();
        $num1 = count($res11);
        $res22 = $goods->where(array('carbody'=>$carbody2,'status'=>1,'goods_type'=>0))->select();
        $num2 = count($res22);
        $res33 = $goods->where(array('carbody'=>$carbody3,'status'=>1,'goods_type'=>0))->select();
        $num3 = count($res33);
        $code = array('status_code'=>'1','carbodys'=>array('carbody1'=>array('num'=>$num1,'carbody1'=>$carbody1,'good1'=>$res1),'carbody2'=>array('num'=>$num2,'carbody2'=>$carbody2,'good2'=>$res2),'carbody3'=>array('num'=>$num3,'carbody3'=>$carbody3,'good3'=>$res3)));
        echo arr_json($code);
    }
     
    //近期活动
    public function activity() {
        $activity = M('activity');        
        $user_id = $_REQUEST['user_id'];//此参数用来判断是否收藏
        //判断是否收藏
        if (!empty($user_id)) {
            $ress = M('user')->where(array('id'=>$user_id,'status'=>'1'))->select();
            $collect_id = $ress['0']['collect_id'];
            $ress1 = M('collect')->where(array('id'=>array('in',$collect_id)))->select();
            foreach ($ress1 as $k1=>$v1) {
                $str_collect .= $v1['type_id'].',';
            }
            $new_str = substr($str_collect,0,-1);
            $new_arr = explode(',',$new_str);
            $res = M('activity')->where(array('status'=>'1'))->order('create_time desc')->limit(2)->select();
            foreach ($res as $k=>$v) {
                if (in_array($v['id'],$new_arr)) {
                    $res[$k]['is_collect'] = '1';
                } else {
                    $res[$k]['is_collect'] = '0';
                }
                $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
                $res[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                $res[$k]['last_update_time'] = date('Y-m-d H:i:s',$v['last_update_time']);
            }
            $code = array('status_code'=>'1','activity'=>$res);
            
        } else {
            $res = M('activity')->where(array('status'=>'1'))->order('create_time desc')->limit(2)->select();
             foreach ($res as $k=>$v) {
                $res[$k]['is_collect'] = '0';
                $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
                $res[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                $res[$k]['last_update_time'] = date('Y-m-d H:i:s',$v['last_update_time']);
            }
            $code = array('status_code'=>'1','activity'=>$res);
           
        }
        
        $this ->ajaxReturn($code);
    }
    
    //按品牌
    public function brand() {      
        $goods_cate = M('goods_cate');
        //获取所有顶级品牌
        $condition_cate['pid'] = 0;
        $condition_cate['status'] = 1;
        $cateas = $goods_cate->field('status',true)->where($condition_cate)->select();
        //根据品牌所属首字母排序
        $cate_index = array();
        foreach ($cateas as $key => $cate_info) {
            $c_index = getFirstCharter($cate_info['name']);
            if (!$c_index)
                continue;
            $cate_index[$c_index][] = $cate_info;
        }
        //ksort对数组按照键名从小到大排序
        ksort($cate_index);
        //车品牌按英文字母排序
        $res = $goods_cate->field('id,name,sort,thumb')->where('is_hot=1')->order('sort desc')->limit(5)->select();
        foreach($res as $k=>$v) {
            $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
        }
        $code = array('status_code'=>'1','is_hot'=>$res,'brand_first'=>$cate_index);
        echo arr_json($code);           
    }
    
    //按品牌列表
    public function brandList() {  
        $goods = M('goods');
        $brand_id = $_REQUEST['brand_id'];
        //$brand_id = 2;
        $res = $goods->field('id,name,price,thumb,sort,click')->where(array('cate_id'=>$brand_id,'status'=>'1','goods_type'=>0))->order('sort desc')->select();
        foreach($res as $k=>$v) {
            $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
        }
        $res1 = $goods->field('name as brand_name,thumb as brand_thumb')->table(C('DB_PREFIX').'goods_cate')->where('id='.$brand_id)->find();
        $res1['brand_thumb'] = C('HTTP_HEAD').$res1['brand_thumb'];
        if (empty($brand_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
        } else {
            $code = array('status_code'=>'1','brand'=>$res1,'brand_list'=>$res);
            echo arr_json($code);
        }
    }
    
    //按条件查询  价格，级别，驱动，国家，可以都不选
    public function search() {
        $goods = M('goods');
        //价格区间
        $min = $_REQUEST['min'];
        $max = $_REQUEST['max'];  
        if (empty($min) && empty($max)) {
            $where['price'] = array('neq','.....');
        } else if (empty($min) && !empty($max)) {
            $price = array(array('gt',0),array('lt',$max),'and');
            $where['price'] = $price;
        } else if (empty($max) && !empty($min)) {
            $price = array(array('gt',0),array('lt',$min),'and');
            $where['price'] = $price;
        } else {
            $price = array(array('gt',$min),array('lt',$max),'and');
            if ($price == '不限' || $price == '') {
                $where['price'] = array('neq','.....');
            } else {
                $where['price'] = $price;
            }
        }     
        //车级别
        $carbody = $_REQUEST['carbody'];
        if (empty($carbody) || $carbody == '不限' || $carbody == '' || $carbody == '不限车身') {
            $where['carbody'] = array('neq','.....');
        } else {
            $where['carbody'] = $carbody;
        }
        //驱动
        $drive = $_REQUEST['drive'];
        if (empty($drive) || $drive == '不限' || $drive == '' || $drive == '不限驱动') {
            $where['drive'] = array('neq','.....');
        } else {
            $where['drive'] = $drive;
        }
        //出产国家
        $country = $_REQUEST['country'];
        if (empty($country) || $country == '不限' || $country == '' || $country == '不限国家') {
            $where['country'] = array('neq','.....');
        } else {
            $where['country'] = $country;
        }
        //必要条件
        $where['status'] = 1;
        $where['goods_type'] = 0;
        //查询
        $res = $goods->field('id,name,price,hot,carbody,drive,country,thumb,sort,click,label')->where($where)->order('sort desc')->select();
        foreach($res as $k=>$v) {
            $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
        }
        if ($res) {
            $code = array('status_code'=>'1','cars'=>$res);
            echo arr_json($code);
        } else {
            $code = array('status_code'=>'2','result_msg'=>'搜索不到满足条件的车辆');
            echo arr_json($code);
        }  
    }
    
    //按关键字查询
    public function keyword() {
        $goods = M('goods');
        $search = M('search');
        $keyword = $_REQUEST['keyword'];
        //$keyword = '宝马';
        //关键字查询
        if (empty($keyword)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
        } else {
            //记录搜索字段
            $data['search'] = $keyword;
            $data['create_time'] = time();
            $search->add($data);
            $where['name'] = array('like','%'.$keyword.'%');
            $where['status'] = '1';
            $where['goods_type'] = 0;
            $res = $goods->field('id,name,price,thumb,label,click')->where($where)->order('click desc')->select();
            foreach($res as $k=>$v) {
                $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
            }
            if ($res) {           
                    $code = array('status_code'=>'1','goods'=>$res);                            
                    echo arr_json($code); 
            } else {
                $code = array('status_code'=>'3','result_msg'=>'搜索不到满足条件的车辆');
                echo arr_json($code);
            }          
        }     
    }
    
    //为你推荐
    public function forYouRec() {
        $goods = M('goods');
        $data = $_REQUEST['cate_id'];
        //$data = '2';
        if (empty($data)) {
            $res = $goods->field('id,name,cate_id,price,thumb')->where(array('status'=>'1','goods_type'=>0))->order('click desc')->limit(2)->select();
            foreach($res as $k=>$v) {
                $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
            }
            $code = array('status_code'=>'1','recommend'=>$res);
            echo arr_json($code);
        } else {
            //商品推荐的条件
            $condition['cate_id'] = $data;
            $condition['status'] = '1';
            $condition['goods_type'] = 0;
            //查询
            $res = $goods->field('id,name,cate_id,price,thumb')->where($condition)->limit(3)->select();
            foreach($res as $k=>$v) {
                $res[$k]['deposit'] = $v['price'].'万';
                $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
                unset($res[$k]['price']);
            }
            $code = array('status_code'=>'1','recommend'=>$res);
            echo arr_json($code);
        }
        
    }  
    
    //预约看车(增加)
    public function preBookCarAdd() {
        $prebookcar = M('prebookcar');
        $user_id = $_REQUEST['user_id'];
        $goods_id = $_REQUEST['goods_id'];
        $booker = $_REQUEST['booker'];
        $telephone = $_REQUEST['telephone'];
        $book_time = $_REQUEST['book_time'];
        $book_time = strtotime($book_time);
        $store = $_REQUEST['store'];
        $res = $prebookcar->table(C('DB_PREFIX').'goods')->where(array('id'=>$goods_id,'status'=>'1','goods_type'=>0))->select();
        $name = $res['0']['name'];
        $thumb = $res['0']['thumb'];
        //数组
        $data['name'] = $name;
        $data['thumb'] = $thumb;
        $data['booker'] = $booker;
        $data['telephone'] = $telephone;
        $data['book_time'] = $book_time;
        $data['store'] = $store;
        $data['goods_id'] = $goods_id;
        $data['status'] = '1';
        $data['create_time'] = time();
        if (empty($user_id)) {//未登录状态
            if (empty($goods_id) || empty($booker) || empty($telephone) || empty($book_time) || empty($store)) {
                $code = array('status_code'=>'2','result_msg'=>'缺少参数');
                echo arr_json($code);
            }else {
                $res = $prebookcar->where(array('telephone'=>$telephone,'goods_id'=>$goods_id,'booker'=>$booker,'status'=>'1'))->select();
                if ($res) {
                    $code = array('status_code'=>'3','result_msg'=>'这辆车,您已预定！');
                    echo arr_json($code);
                } else {
                    $list = $prebookcar->add($data);
                    if ($list) {
                        $code = array('status_code'=>'1');
                        echo arr_json($code);
                    } else {
                        $code = array('status_code'=>'0','result_msg'=>'服务器有异常');
                        echo arr_json($code);
                    }            
                } 
            }
        } else {//已登录状态          
            $res2 = $prebookcar->where(array('user_id'=>$user_id,'goods_id'=>$goods_id,'status'=>'1'))->select();
            if ($res2) {
                $code = array('status_code'=>'3','result_msg'=>'这辆车,您已预定！');
                echo arr_json($code);
            } else {
                $data['user_id'] = $user_id;
                $list = $prebookcar->add($data);
                if ($list) {
                        $code = array('status_code'=>'1');
                        echo arr_json($code);
                } else {
                        $code = array('status_code'=>'0','result_msg'=>'服务器有异常');
                        echo arr_json($code);
                } 
            }
        }      
    }
    
    //预约看车(删除)
    public function preBookCarDel() {
        $prebookcar = M('prebookcar');
        //$user_id = '2374';
        $user_id = $_REQUEST['user_id'];
        //$goods_id = '283';
        $goods_id = $_REQUEST['goods_id'];
        $res = $prebookcar->where("user_id='{$user_id}' and goods_id='{$goods_id}'")->select();
        $id = $res['0']['id'];
        if (empty($user_id) || empty($goods_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
        } else {
            $list = $prebookcar->where('id='.$id)->delete();
            if ($list) {
                $code = array('status_code'=>'1');
                echo arr_json($code);
            } else {
                $code = array('status_code'=>'3','result_msg'=>'车辆不存在');
                echo arr_json($code);
            }
        }      
    }
    
    //预约看车(取消)
    public function preBookCarCancel() {
        $user_id = $_REQUEST['user_id'];
        $goods_id = $_REQUEST['goods_id'];
        if (empty($user_id) || empty($goods_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $list = M('prebookcar')->where(array('user_id'=>$user_id,'goods_id'=>$goods_id))->save(array('status'=>'4'));
        if (!$list) {
            $code = array('status_code'=>'3','result_msg'=>'取消预约失败');
            echo arr_json($code);
            exit;
        }
        $code = array('status_code'=>'1');
        echo arr_json($code);
    }
    
    //预约看车(修改)
    public function preBookCarSave() {
        $prebookcar = M('prebookcar');
        //$user_id = '2374';
        $user_id = $_REQUEST['user_id'];
        //$goods_id = '281';
        $goods_id = $_REQUEST['goods_id'];
        $booker = $_REQUEST['booker'];
        $telephone = $_REQUEST['telephone'];
        $book_time = $_REQUEST['book_time'];
        $book_time = strtotime($book_time);
        $store = $_REQUEST['store'];
        if (empty($user_id) || empty($goods_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
        } else {
            if (!empty($booker)) {
                $prebookcar->booker = $booker;
            }
            if (!empty($telephone)) {
                $prebookcar->telephone = $telephone;
            }
            if (!empty($book_time)) {
                $prebookcar->book_time = $book_time;
            }
            if (!empty($store)) {
                $prebookcar->store = $store;
            }
            $list = $prebookcar->where("user_id='{$user_id}' and goods_id='{$goods_id}'")->save();
            if ($list) {
                $code = array('status_code'=>'1');
                echo arr_json($code);
            } else {
                $code = array('status_code'=>'3','result_msg'=>'没做任何修改');
                echo arr_json($code);
            }
        }   
    }
    
    //预约看车(查询)
    public function preBookCarSel() {
         $prebookcar = M('prebookcar');
         $user_id = $_REQUEST['user_id'];
         $res = $prebookcar->field('id,name,thumb,booker,book_time,goods_id,store,status')->where(array('user_id'=>$user_id,'status'=>array('neq','4')))->order('status asc')->select();
         foreach($res as $k=>$v) {
            $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
            if ($v['book_time'] < time() && $v['status'] == '1') {
                $res[$k]['status'] = '2';
                $prebookcar->where(array('id'=>$res[$k]['id']))->save(array('status'=>'2'));
            }
         }
         if (empty($user_id)) {
             $code = array('status_code'=>'2','result_msg'=>'缺少参数');
             echo arr_json($code);
         } else {
             if ($res) {
                 $code = array('status_code'=>'1','pre_book_cars'=>$res);
                 echo arr_json($code);
             } else {
                 $code = array('status_code'=>'3','pre_book_cars'=>array());
                 echo arr_json($code);
             }
         }
    }
    
    //限时购banner
     public function seckillBanner1() {
        $seckill_goods = M('seckill_goods as a');
        $seckill_set = M('seckill_set');
        $time = time();
        $res = $seckill_goods->table(C('DB_PREFIX').'seckill_set')->where("status=1")->order('id desc')->select();
        //默认最后一个场次
        //第五场 开始时间2017-04-01 00:00:00  结束时间2017-04-01 05:00:00 
        $id = $res['0']['id'];
        $start_time = $res['0']['start_time'];
        $end_time = $res['0']['end_time'];
        $start_date = date('Y-m-d H:i:s',$start_time);
        $end_date = date('Y-m-d H:i:s',$end_time);
        if ($start_time > $time) {
            $seckill_set->subject = '秒杀预告：预计'.$start_date.'开始';
            $list = $seckill_set->where('id='.$id)->save();                
        } else if ($start_time < $time && $end_time > $time) {
            $seckill_set->subject = '秒杀进行中:预计'.$end_date.'结束';
            $list = $seckill_set->where('id='.$id)->save();
        } else if ($end_time < $time) {
            $seckill_set->subject = "秒杀已结束";
            $list = $seckill_set->where('id='.$id)->save();
            exit;
        }
        $res1 = $seckill_goods->field('a.id,a.goods_id,a.seckill_price,a.quantity,a.cover_img,a.sale_num,b.name,b.price,b.thumb,c.subject,c.start_time,c.end_time')->join(C('DB_PREFIX').'goods as b on a.goods_id=b.id')->join(C('DB_PREFIX').'seckill_set as c on a.seckill_id=c.id')->where('a.seckill_id='.$id)->order('b.sort desc')->limit(1)->select();  
        foreach ($res1 as $k=>$v) {
            $res1[$k]['cover_img'] = C('HTTP_HEAD').$v['cover_img'];
            $res1[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
            $res1[$k]['start_time'] = date('Y-m-d H:i:s',$v['start_time']);
            $res1[$k]['end_time'] = date('Y-m-d H:i:s',$v['end_time']); 
        }           
        $code = array('status_code'=>'1','seckill_banner'=>$res1);
        echo arr_json($code);    
    }
    
    //限时购banner车辆 新的
    public function seckillBanner() {
        
        $seckill_goods = M('seckill_goods as a');
        $seckill_set = M('seckill_set');
        $time = time();
        $res = $seckill_set->where(array('status'=>1,'seckill_type'=>0))->select();
        $id = $res[0]['id'];
        $start_time = $res['0']['start_time'];
        $end_time = $res['0']['end_time'];
        $start_date = date('Y-m-d H:i:s',$start_time);
        $end_date = date('Y-m-d H:i:s',$end_time);
        if ($start_time > $time) {
            $seckill_set->subject = '秒杀预告：预计'.$start_date.'开始';
            $list = $seckill_set->where('id='.$id)->save();                
        } else if ($start_time < $time && $end_time > $time) {
            $seckill_set->subject = '秒杀进行中:预计'.$end_date.'结束';
            $list = $seckill_set->where('id='.$id)->save();
        } else if ($end_time < $time) {
            $seckill_set->subject = "秒杀已结束";
            $list = $seckill_set->where('id='.$id)->save();   
            exit;
        }
        $res1 = $seckill_goods->field('a.id,a.goods_id,a.seckill_price,a.quantity,a.cover_img,a.sale_num,b.name,b.price,b.thumb,c.subject,c.start_time,c.end_time')->join(C('DB_PREFIX').'goods as b on a.goods_id=b.id')->join(C('DB_PREFIX').'seckill_set as c on a.seckill_id=c.id')->where('a.seckill_id='.$id)->order('b.sort desc')->limit(1)->select();  
        foreach ($res1 as $k=>$v) {
            $res1[$k]['cover_img'] = C('HTTP_HEAD').$v['cover_img'];
            $res1[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
            $res1[$k]['start_time'] = date('Y-m-d H:i:s',$v['start_time']);
            $res1[$k]['end_time'] = date('Y-m-d H:i:s',$v['end_time']); 
        }           
        $code = array('status_code'=>'1','seckill_banner'=>$res1);
        echo arr_json($code);
        
    }
    
    //限时购banner车品 新的
    public function seckillBannerCarproduct() {
        
        $seckill_goods = M('seckill_goods as a');
        $seckill_set = M('seckill_set');
        $res = $seckill_set->where(array('status'=>1,'seckill_type'=>1))->select();
        $id = $res[0]['id'];
        $start_time = $res['0']['start_time'];
        $end_time = $res['0']['end_time'];
        $start_date = date('Y-m-d H:i:s',$start_time);
        $end_date = date('Y-m-d H:i:s',$end_time);
        if ($start_time > $time) {
            $seckill_set->subject = '秒杀预告：预计'.$start_date.'开始';
            $list = $seckill_set->where('id='.$id)->save();                
        } else if ($start_time < $time && $end_time > $time) {
            $seckill_set->subject = '秒杀进行中:预计'.$end_date.'结束';
            $list = $seckill_set->where('id='.$id)->save();
        } else if ($end_time < $time) {
            $seckill_set->subject = "秒杀已结束";
            $list = $seckill_set->where('id='.$id)->save();
            exit;
        }
        $res1 = $seckill_goods->field('a.id,a.goods_id,a.seckill_price,a.quantity,a.cover_img,a.sale_num,b.name,b.price,b.thumb,c.subject,c.start_time,c.end_time')->join(C('DB_PREFIX').'goods as b on a.goods_id=b.id')->join(C('DB_PREFIX').'seckill_set as c on a.seckill_id=c.id')->where('a.seckill_id='.$id)->order('b.sort desc')->limit(1)->select();  
        foreach ($res1 as $k=>$v) {
            $res1[$k]['cover_img'] = C('HTTP_HEAD').$v['cover_img'];
            $res1[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
            $res1[$k]['start_time'] = date('Y-m-d H:i:s',$v['start_time']);
            $res1[$k]['end_time'] = date('Y-m-d H:i:s',$v['end_time']); 
        }           
        $code = array('status_code'=>'1','seckill_banner'=>$res1);
        echo arr_json($code);
        
    }
    
     //限时抢购列表
    public function seckillList() {

        $seckillSetModel = D('SeckillSet');
        $seckillGoodsModel = D('SeckillGoods');
        $goodsModel = D('Goods');

        $seckillSetData = $seckillSetModel->isSeckill(0);
        //默认最后一个场次
        //第五场 开始时间2017-04-01 00:00:00  结束时间2017-04-01 05:00:00 
        $returnArray = array();
        $status = $seckillSetData['status'];
        $returnArray['status_code'] = 1;
        $returnArray['status'] = $status;
        if ($status == 1 || $status == 2) {
            
            $id = $seckillSetData['seckill_id'];
            $start_time = date('Y/m/d H:i:s',$seckillSetData['start_time']);
            $end_time = date('Y/m/d H:i:s',$seckillSetData['end_time']);
           
            $seckillGoodsList = $seckillGoodsModel->getList(array(seckill_id => $id),'id,goods_id,seckill_price as price,quantity,sale_num','id asc'); 
            foreach ($seckillGoodsList as $k=>$v) {
                $goods_id = $v['goods_id'];
                $goodsData = $goodsModel -> getOne(array(id => $goods_id),'name,thumb,sort,hot,deposit');
                $seckillGoodsList[$k]['name'] = $goodsData['name'];
                $seckillGoodsList[$k]['thumb'] = $goodsData['thumb'];
                $seckillGoodsList[$k]['sort'] = $goodsData['sort'];
                $seckillGoodsList[$k]['hot'] = $goodsData['hot'];
                $seckillGoodsList[$k]['deposit'] = $goodsData['deposit'];
            }
            $returnArray['seckill_id'] = $id;
            
            $returnArray['start_time'] = $start_time;
            $returnArray['end_time'] = $end_time;
            $returnArray['seckill_goods'] = $seckillGoodsList;
            
        }  else {

            $seckillSetModel->update(array(id => $seckillSetData['seckill_id']),array(subject => "秒杀已结束",status => 0));
            $returnArray['seckill_goods'] = array();
        }
                   
        
        $this ->ajaxReturn($returnArray);
             
    }
    
    //限时抢购详情
    public function seckGoodsDetail11() {
        $seckill_goods = M('seckill_goods as a');
        $seckill_set = M('seckill_set');
        $order = M('order');
        $goods = M('goods');
        $goods_id = $_REQUEST['goods_id'];
        $user_id = $_REQUEST['user_id'];//可以不传，用来判断是否已收藏
        //判断是否收藏
        if (!empty($user_id)) {
            $ress = M('user')->where(array('id'=>$user_id))->select();
            $collect_id = $ress['0']['collect_id'];
            $ress1 = M('collect')->where(array('id'=>array('in',$collect_id)))->select();
            foreach ($ress1 as $k11=>$v11) {
                $str_collect .= $v11['type_id'].',';
            }
            $new_str = substr($str_collect,0,-1);
            $new_arr = explode(',',$new_str);
            if (in_array($goods_id,$new_arr)) {//1为已收藏
                $is_collect = '1';
            } else {
                $is_collect = '0';
            }
        } else {
            $is_collect = '0';
        }
        //微信分享
        $url = $_REQUEST['url'];
        $pay_config = D('PayConfig');
        $wx_config_data = $pay_config -> getOneList(array(pay_status => 0,use_status => 1), 'app_id,mch_id,key,app_secret');
        if(empty($wx_config_data)) {
            $jsSdk = new PublicClass1_0_0\WechatClass();
        } else {
            $jsSdk = new PublicClass1_0_0\WechatClass($wx_config_data['app_id'],$wx_config_data['mch_id'],$wx_config_data['key'],$wx_config_data['app_secret']);
        }
        $wx = $jsSdk ->getShareConfig($url);        

        if (empty($goods_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
        } else {
            $res = $seckill_goods->table(C('DB_PREFIX').'seckill_set')->where("status=1 and seckill_type=0")->order('id desc')->select();
            $seckill_set = $res['0']['id'];
            $res1 = $seckill_goods->where('goods_id='.$goods_id.' and seckill_id = '.$seckill_set)->select();
            if ($res1) {//秒杀商品
                
                $id = $res1[0]['id'];
                $key = 'seckill_list_'.$id;
                $redis = A('Redis');
                $redisStock = $redis -> getRedisStock($key);  //获取redis库存
                
                $res2 = $seckill_goods->field('a.seckill_id,a.seckill_price,a.quantity,a.sale_num,a.cover_img,b.subject,b.start_time,b.end_time,c.id,c.name,c.cate_id,c.deposit,c.price,c.thumb,c.iscurrent,c.version,c.guise,c.click,c.guise,c.label,c.content')->join(C('DB_PREFIX').'seckill_set as b on a.seckill_id=b.id')->join(C('DB_PREFIX').'goods as c on a.goods_id=c.id')->where('a.goods_id='.$goods_id)->select();
                foreach($res2 as $k=>$v) {
                    //guise | 号前面的是外观颜色，后面是内观颜色，要的是外观颜色
                    $arr  = explode('|',$v['guise']);
                    $new_str = $arr[0];
                    $res2[$k]['guise'] = $new_str;
                    $res2[$k]['cover_img'] = C('HTTP_HEAD').$v['cover_img'];
                    $res2[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
                    $res2[$k]['is_collect'] = $is_collect;
                    $res3 = M('goods_color')->where(array('name'=>array('in',$new_str)))->select();
                    $new_array = array();
                    foreach ($res3 as $k3=>$v3) {
                        $new_array[$v3['name']] = $v3['code'];
                    }
                    $res2[$k]['colors'] = $new_array;
                }
                $content = $res2[0]['content'];
                $content1 = str_replace('"',"'", $content);
                $colors = change_color($res2);
                $aa = array('content'=>$content1);
                $res3 = $res2[0]+$colors+$aa;
                $res4 = str_replace('"',"'", $res3);
                $res4['quantity'] = $redisStock;              //redis库存
                $code = array('status_code'=>'1','goods'=>$res4);
            } else {//非秒杀
                $res2 = $seckill_goods->field('id,name,cate_id,deposit,price,sale_price,thumb,iscurrent,version,guise,click,is_oneprice,label,content')->table(C('DB_PREFIX').'goods')->where(array('id'=>$goods_id,'is_oneprice'=>1,'status'=>1,'goods_type'=>0))->select();                
                if ($res2) {//一口价
                    //guise | 号前面的是外观颜色，后面是内观颜色，要的是外观颜色
                    foreach($res2 as $k=>$v) {
                        $arr  = explode('|',$v['guise']);
                        $new_str = $arr[0];
                        $res2[$k]['guise'] = $new_str;
                        $res2[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
                        $res2[$k]['is_collect'] = $is_collect;
                        $res3 = M('goods_color')->where(array('name'=>array('in',$new_str)))->select();
                        $new_array = array();
                        foreach ($res3 as $k3=>$v3) {
                            $new_array[$v3['name']] = $v3['code'];
                        }
                        $res2[$k]['colors'] = $new_array;
                    }
                    $content = $res2[0]['content'];
                    $content1 = str_replace('"',"'", $content);
                    $colors = change_color($res2);
                    $aa = array('content'=>$content1);
                    $res3 = $res2[0]+$colors+$aa;
                    $res4 = str_replace('"',"'", $res3);
                    $code = array('status_code'=>'1','goods'=>$res4);
                } else {//普通价
                    $res2 = $seckill_goods->field('id,name,cate_id,deposit,price,thumb,iscurrent,version,guise,click,label,content')->table(C('DB_PREFIX').'goods')->where(array('id'=>$goods_id,'status'=>1,'goods_type'=>0))->select();
                    foreach($res2 as $k=>$v) {
                        //guise | 号前面的是外观颜色，后面是内观颜色，要的是外观颜色
                        $arr  = explode('|',$v['guise']);
                        $new_str = $arr[0];
                        $res2[$k]['guise'] = $new_str;
                        $res2[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
                        $res2[$k]['is_collect'] = $is_collect;
                        $res3 = M('goods_color')->where(array('name'=>array('in',$new_str)))->select();
                        $new_array = array();
                        foreach ($res3 as $k3=>$v3) {
                            $new_array[$v3['name']] = $v3['code'];
                        }
                        $res2[$k]['colors'] = $new_array;
                    }
                    $content = $res2[0]['content'];
                    $content1 = str_replace('"',"'", $content);
                    $colors = change_color($res2);
                    $aa = array('content'=>$content1);
                    $res3 = $res2[0]+$colors+$aa;
                    $res4 = str_replace('"',"'", $res3);
                    $code = array('status_code'=>'1','goods'=>$res4);
                }       
            }            
            $code['wx'] = $wx;
            //点击量+1，只有在车辆详情才有
            $goods->where(array('id'=>$goods_id,'status'=>'1','goods_type'=>0))->setInc('click',1);
            //最后点击时间更新
            $goods->last_click_time = time();
            $goods->where(array('id'=>$goods_id))->save();
            echo arr_json($code);
        }
    }
    
    //车辆详情，新的
    public function seckGoodsDetail() {
        
        $seckill_goods = M('seckill_goods as a');
        $seckill_set = M('seckill_set');
        $order = M('order');
        $goods = M('goods');
        $goods_id = $_REQUEST['goods_id'];
        $user_id = $_REQUEST['user_id'];//可以不传，用来判断是否已收藏
        //判断是否收藏
        if (!empty($user_id)) {
            $ress = M('user')->where(array('id'=>$user_id))->select();
            $user_id = $ress['0']['id'];
            $ress1 = M('collect')->where(array('user_id'=>$user_id,type_id => $goods_id))->select();
            
            if (!empty($ress1)) {//1为已收藏
                $is_collect = '1';
            } else {
                $is_collect = '0';
            }
        } else {
            $is_collect = '0';
        }
        //微信分享
        $url = $_REQUEST['url'];
        $pay_config = D('PayConfig');
        $wx_config_data = $pay_config -> getOneList(array(pay_status => 0,use_status => 1), 'app_id,mch_id,key,app_secret');
        if(empty($wx_config_data)) {
            $jsSdk = new PublicClass1_0_0\WechatClass();
        } else {
            $jsSdk = new PublicClass1_0_0\WechatClass($wx_config_data['app_id'],$wx_config_data['mch_id'],$wx_config_data['key'],$wx_config_data['app_secret']);
        }
        $wx = $jsSdk ->getShareConfig($url);        

        if (empty($goods_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            exit(arr_json($code));
        }
        $res = $seckill_goods->table(C('DB_PREFIX').'seckill_set')->where("status=1 and seckill_type=0")->order('id desc')->select();
        $seckill_set = $res['0']['id'];
        $res1 = $seckill_goods->where('goods_id='.$goods_id.' and seckill_id = '.$seckill_set)->select();
        $seckillSet = new \Home\Model\SeckillSetModel();
        $is_seckill = $seckillSet->checkGoodsIsSeckill($goods_id);//判断是否秒杀商品，status 0-没有秒杀，1-秒杀进行中，2-秒杀未开启
        if ($is_seckill['status'] == 0) {//非秒杀车辆
            $res2 = $seckill_goods->field('id,name,cate_id,deposit,price,sale_price,thumb,iscurrent,version,guise,click,is_oneprice,label,content')->table(C('DB_PREFIX').'goods')->where(array('id'=>$goods_id,'is_oneprice'=>1,'status'=>1,'goods_type'=>0))->select();                
                if ($res2) {//一口价
                    //guise | 号前面的是外观颜色，后面是内观颜色，要的是外观颜色
                    foreach($res2 as $k=>$v) {
                        $arr  = explode('|',$v['guise']);
                        $new_str = $arr[0];
                        $res2[$k]['guise'] = $new_str;
                        $res2[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
                        $res2[$k]['is_collect'] = $is_collect;
                        $res3 = M('goods_color')->where(array('name'=>array('in',$new_str)))->select();
                        $new_array = array();
                        foreach ($res3 as $k3=>$v3) {
                            $new_array[$v3['name']] = $v3['code'];
                        }
                        $res2[$k]['colors'] = $new_array;
                    }
                    $content = $res2[0]['content'];
                    $content1 = str_replace('"',"'", $content);
                    $colors = change_color($res2);
                    $aa = array('content'=>$content1);
                    $res3 = $res2[0]+$colors+$aa;
                    $res4 = str_replace('"',"'", $res3);
                    $code = array('status_code'=>'1','goods'=>$res4);
                } else {//普通价
                    $res2 = $seckill_goods->field('id,name,cate_id,deposit,price,thumb,iscurrent,version,guise,click,label,content')->table(C('DB_PREFIX').'goods')->where(array('id'=>$goods_id,'status'=>1,'goods_type'=>0))->select();
                    foreach($res2 as $k=>$v) {
                        //guise | 号前面的是外观颜色，后面是内观颜色，要的是外观颜色
                        $arr  = explode('|',$v['guise']);
                        $new_str = $arr[0];
                        $res2[$k]['guise'] = $new_str;
                        $res2[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
                        $res2[$k]['is_collect'] = $is_collect;
                        $res3 = M('goods_color')->where(array('name'=>array('in',$new_str)))->select();
                        $new_array = array();
                        foreach ($res3 as $k3=>$v3) {
                            $new_array[$v3['name']] = $v3['code'];
                        }
                        $res2[$k]['colors'] = $new_array;
                    }
                    $content = $res2[0]['content'];
                    $content1 = str_replace('"',"'", $content);
                    $colors = change_color($res2);
                    $aa = array('content'=>$content1);
                    $res3 = $res2[0]+$colors+$aa;
                    $res4 = str_replace('"',"'", $res3);
                    $code = array('status_code'=>'1','goods'=>$res4);
                }       
        } else if ($is_seckill['status'] == 1) {//秒杀车辆
                $id = $res1[0]['id'];
                $key = 'seckill_list_'.$id;
                $redis = A('Redis');
                $redisStock = $redis -> getRedisStock($key);  //获取redis库存
                
                $res2 = $seckill_goods->field('a.seckill_id,a.seckill_price,a.quantity,a.sale_num,a.cover_img,b.subject,b.start_time,b.end_time,c.id,c.name,c.cate_id,c.deposit,c.price,c.thumb,c.iscurrent,c.version,c.guise,c.click,c.guise,c.label,c.content')->join(C('DB_PREFIX').'seckill_set as b on a.seckill_id=b.id')->join(C('DB_PREFIX').'goods as c on a.goods_id=c.id')->where('a.goods_id='.$goods_id)->select();
                foreach($res2 as $k=>$v) {
                    //guise | 号前面的是外观颜色，后面是内观颜色，要的是外观颜色
                    $arr  = explode('|',$v['guise']);
                    $new_str = $arr[0];
                    $res2[$k]['guise'] = $new_str;
                    $res2[$k]['cover_img'] = C('HTTP_HEAD').$v['cover_img'];
                    $res2[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
                    $res2[$k]['is_collect'] = $is_collect;
                    $res3 = M('goods_color')->where(array('name'=>array('in',$new_str)))->select();
                    $new_array = array();
                    foreach ($res3 as $k3=>$v3) {
                        $new_array[$v3['name']] = $v3['code'];
                    }
                    $res2[$k]['colors'] = $new_array;
                }
                $content = $res2[0]['content'];
                $content1 = str_replace('"',"'", $content);
                $colors = change_color($res2);
                $aa = array('content'=>$content1);
                $res3 = $res2[0]+$colors+$aa;
                $res4 = str_replace('"',"'", $res3);
                $res4['quantity'] = $redisStock;              //redis库存
                $code = array('status_code'=>'1','goods'=>$res4);
        } else {//秒杀预告
             $id = $res1[0]['id'];
            $key = 'seckill_list_'.$id;
            $redis = A('Redis');
            $redisStock = $redis -> getRedisStock($key);  //获取redis库存

            $res2 = $seckill_goods->field('a.seckill_id,a.seckill_price,a.quantity,a.sale_num,a.cover_img,b.subject,b.start_time,b.end_time,c.id,c.name,c.cate_id,c.deposit,c.price,c.thumb,c.iscurrent,c.version,c.guise,c.click,c.guise,c.label,c.content')->join(C('DB_PREFIX').'seckill_set as b on a.seckill_id=b.id')->join(C('DB_PREFIX').'goods as c on a.goods_id=c.id')->where('a.goods_id='.$goods_id)->select();
            foreach($res2 as $k=>$v) {
                //guise | 号前面的是外观颜色，后面是内观颜色，要的是外观颜色
                $arr  = explode('|',$v['guise']);
                $new_str = $arr[0];
                $res2[$k]['guise'] = $new_str;
                $res2[$k]['cover_img'] = C('HTTP_HEAD').$v['cover_img'];
                $res2[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
                $res2[$k]['is_collect'] = $is_collect;
                $res3 = M('goods_color')->where(array('name'=>array('in',$new_str)))->select();
                $new_array = array();
                foreach ($res3 as $k3=>$v3) {
                    $new_array[$v3['name']] = $v3['code'];
                }
                $res2[$k]['colors'] = $new_array;
            }
            $content = $res2[0]['content'];
            $content1 = str_replace('"',"'", $content);
            $colors = change_color($res2);
            $aa = array('content'=>$content1);
            $res3 = $res2[0]+$colors+$aa;
            $res4 = str_replace('"',"'", $res3);
            $res4['quantity'] = $redisStock;              //redis库存
            $code = array('status_code'=>'1','goods'=>$res4);
            $res4['seckill_id'] = $is_seckill['seckill_id'];
            $res4['start_time']=$is_seckill['start_time'];               
            $res4['is_seckill'] = 2; 
            $code = array('status_code'=>'1','goods'=>$res4);
        }
        $code['wx'] = $wx;
        //点击量+1，只有在车辆详情才有
        $goods->where(array('id'=>$goods_id,'status'=>'1','goods_type'=>0))->setInc('click',1);
        //最后点击时间更新
        $goods->last_click_time = time();
        $goods->where(array('id'=>$goods_id))->save();
        $this ->ajaxReturn($code);
        //exit(arr_json($code));
        
    }
    
    //更新秒杀抢购数量（支付成功后执行）
    public function updateSaleNum () {
        $seckill_goods = M('seckill_goods');
        //$goods_id = '291';
        $goods_id = $_REQUEST['goods_id'];
        if (empty($goods_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $seckill_goods->where(array('goods_id'=>$goods_id))->setInc('sale_num',1);//销售数量加1
        $res = $seckill_goods->where(array('goods_id'=>$goods_id))->select();
        if ($res['0']['sale_num'] > $res['0']['quantity']) {
            $code = array('status_code'=>'3','result_msg'=>'库存不足');
            echo arr_json($code);
            exit;
        }
        $code = array('status_code'=>'1');
        echo arr_json($code);
    }
    
    //详情促销
    public function promotion() {
            $goods = M('goods');
            $goods_id = $_REQUEST['goods_id'];
            //$goods_id = '281';
            $res = $goods->field('id,promotion_id')->where(array('id'=>$goods_id,'status'=>1,'goods_type'=>0))->select();
            $promotion_id = $res['0']['promotion_id'];
            $where['id'] = array('in',$promotion_id);
            $res1 = $goods->table(C('DB_PREFIX').'promotion')->where($where)->select();
            if (empty($goods_id)) {
                $code = array('status_code'=>'2','result_msg'=>'缺少参数');
                echo arr_json($code);
            } else {
                if ($res1) {
                    $code = array('status_code'=>'1','promotion'=>$res1);
                    echo arr_json($code);
                } else {
                    $code = array('status_code'=>'3','result_msg'=>'暂无促销信息');
                    echo arr_json($code);
                }
            }        
    }
    
    //详情参数（多表查询经典案例）
    public function parameter() {
        $goods_id = $_REQUEST['goods_id'];
        if (empty($goods_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $res = M('carattribute as a')->field('a.name as children,a.pid,b.value')->join('__CARCONFIG__ as b on b.attrid=a.id')->where(array('goodid'=>$goods_id))->select();
        $res1 = M('carattribute')->where(array('pid'=>'0'))->select();
        foreach ($res as $k=>$v) {
            $k1 = $res[$k]['pid']-1;
            $res[$k]['parent'] =  $res1[$k1]['name'];   
        }
        $arr = array();
        foreach ($res as $k2=>$v2) {
            $par = $v2['parent'];
            unset($v2['parent']);
            unset($v2['pid']);
            $arr[$par][] = $v2;
        }
        $code = array('status_code'=>'1','parameters'=>$arr);
        echo arr_json($code);
    }
    
    //一口价
    public function onePrice() {
        $goods = M('goods');
        $res = $goods->field('id,name,price,sale_price,sale_thumb,deposit,sort')->where(array('is_oneprice'=>'1','status'=>'1','goods_type'=>0))->order('sort desc')->limit(2)->select();
        foreach($res as $k=>$v) {
            $res[$k]['thumb'] = C('HTTP_HEAD').$v['sale_thumb'];
        }
        $code = array('status_code'=>'1','one_price'=>$res);
        echo arr_json($code);
    }
    
    /*--------------↓↓违章开始↓↓--------------*/
    
    /**
     * 违章共有三个接口
     */
    
    //违章查询接口
    public function breakRuleSel() {
        $illegal_info = M('illegal_info');
        $vehicle = '粤B301QA';
        $frame_no = '018337';
        $engine_no = '018337';
        $key = <<<KEY
            #s^un2ye31<cn%|aoXpR2015cwt
KEY;
        $key = trim($key);
        $data['vehicle'] = $vehicle;
        $data['frame_no'] = $frame_no;
        $data['engine_no'] = $engine_no;
        $data['create_time'] = time();
        $res = $illegal_info->where("vehicle="."'".$vehicle."'")->select();
        if (!$res) {
            $illegal_info->add($data);
        } 
        //获取QueryText
        $A = 'Vehicle=' . $vehicle . '&VehicleType=02&FrameNo=' . $frame_no . '&EngineNo=' . $engine_no . '&OwnerName=&Md5Key=' . $key;
        $B = trim(strtoupper(md5($A)));
        $C = 'Vehicle=' . $vehicle . '&VehicleType=02&FrameNo=' . $frame_no . '&EngineNo=' . $engine_no . '&OwnerName=&Md5Sign=' . $B;
        $C = iconv("utf-8", "gb2312//IGNORE", $C);
        $query_text = trim(base64_encode($C));
        //获取Sign
        $D = 'CftMerId=438&Command=2&MerchantId=1002&QueryText=' . $query_text . '&ResFormat=json&UserId=EN1_A61E447C7FF746A960DAA70E609577A0&Version=1&Md5Key=' . $key;
        $sign = trim(strtoupper(md5($D)));
        //请求参数数组
        $post_data = array();
        $post_data['Command'] = '2';
        $post_data['UserId'] = 'EN1_A61E447C7FF746A960DAA70E609577A0';
        $post_data['CftMerId'] = '438';
        $post_data['MerchantId'] = '1002';
        $post_data['Version'] = '1';
        $post_data['ResFormat'] = 'json';
        $post_data['Sign'] = $sign;
        $post_data['QueryText'] = $query_text;
        $url = 'http://www.gdwz.net/open_cx/ser_wzcx.aspx?';
        $o = '';
        //拼接参数
        foreach ($post_data as $k => $v) {
            $o .= "$k=" . $v . '&';
        }
        //去掉最后一个&
        $post_data = substr($o, 0, -1);
        //curl get方法
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url . $post_data);
        curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $output = curl_exec($ch);
        $contents = simplexml_load_string($output);
        //var_dump($contents);exit;
        $content = $contents->RespText;
        $content = urldecode(base64_decode($content)); //解码
        $content = iconv("gb2312", "utf-8//IGNORE", $content); //转编码
        $content = explode('&', $content); //分割字符串
        //var_dump($content);exit;
        $content_total = $content[1];
        $content_total = explode('=', $content_total);
        $total_num = $content_total[1];
        $_SESSION['total_num'] = $total_num;
        //return $total_num;
        $content = $content[2];
        $content = explode('=', $content);
        $content = $content[1];
        $xml_content = <<<XML
        <?xml   version="1.0" encoding="utf-8"?>
                $content
XML;

        $contents = trim($xml_content);
        $contents = simplexml_load_string($contents);
        $content = $contents->Record[0]; //取第一条违章记录的所有字段字段    
        //var_dump($contents);
        return $contents;
    }
    
    //违章预查询接口，用来判断违章号能不能办理
    public function breakRulePrepay() {
        $key = <<<KEY
            #s^un2ye31<cn%|aoXpR2015cwt
KEY;
        $key = trim($key);
        //获取QueryText
        $A = 'CftTransId=438&Datetime=2016-10-02 11:24:00&ViolationId=20237024&MailReceipt=0&MailFee=0&FeeType=1&TotalFee=15000&Md5Key=' . $key;
        $B = trim(strtoupper(md5($A)));
        $C = 'CftTransId=438&Datetime=2016-10-02 11:24:00&ViolationId=20237024&MailReceipt=0&MailFee=3&FeeType=1&TotalFee=15000&Md5Sign=' . $B;
        $C = iconv("utf-8", "gb2312//IGNORE", $C);
        $query_text = trim(base64_encode($C));
        //获取Sign
        $D = 'CftMerId=438&Command=2&MerchantId=1002&QueryText=' . $query_text . '&ResFormat=json&UserId=EN1_A61E447C7FF746A960DAA70E609577A0&Version=1&Md5Key=' . $key;
        $sign = trim(strtoupper(md5($D)));
        //请求参数数组
        $post_data = array();
        $post_data['Command'] = '2';
        $post_data['UserId'] = 'EN1_A61E447C7FF746A960DAA70E609577A0';
        $post_data['CftMerId'] = '438';
        $post_data['MerchantId'] = '1002';
        $post_data['Version'] = '1';
        $post_data['ResFormat'] = 'json';
        $post_data['Sign'] = $sign;
        $post_data['QueryText'] = $query_text;
        $url = 'http://www.gdwz.net/open_cx/ser_wzdb.aspx?';
        $o = '';
        //拼接参数
        foreach ($post_data as $k => $v) {
            $o .= "$k=" . $v . '&';
        }
        //去掉最后一个&
        $post_data = substr($o, 0, -1);
        //curl get方法
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url . $post_data);
        curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $output = curl_exec($ch);
        $contents = simplexml_load_string($output);
        var_dump($contents); //当SpRetCode为0000时表示可以办理
    }
    
    //违章代办接口,调用就扣钱了
    public function breakRulePay() {
         $key = <<<KEY
            #s^un2ye31<cn%|aoXpR2015cwt
KEY;
        $key = trim($key);
        //获取QueryText
        $A = 'CftTransId=438SpTransId=1001203735&Datetime=2016-10-02 11:24:00&ViolationId=20237024&MailReceipt=0&FeeType=0&TotalFee=15000&Md5Key=' . $key;
        $B = trim(strtoupper(md5($A)));
        $C = 'CftTransId=438SpTransId=1001203735&Datetime=2016-10-02 11:24:00&ViolationId=20237024&MailReceipt=0&FeeType=0&TotalFee=15000&Md5Sign=' . $key;
        $C = iconv("utf-8", "gb2312//IGNORE", $C);
        $query_text = trim(base64_encode($C));
        //获取Sign
        $D = 'CftMerId=438&Command=2&MerchantId=1002&QueryText=' . $query_text . '&ResFormat=json&UserId=EN1_A61E447C7FF746A960DAA70E609577A0&Version=1&Md5Key=' . $key;
        $sign = trim(strtoupper(md5($D)));
        //请求参数数组
        $post_data = array();
        $post_data['Command'] = '2';
        $post_data['UserId'] = 'EN1_A61E447C7FF746A960DAA70E609577A0';
        $post_data['CftMerId'] = '438';
        $post_data['MerchantId'] = '1002';
        $post_data['Version'] = '1';
        $post_data['ResFormat'] = 'json';
        $post_data['Sign'] = $sign;
        $post_data['QueryText'] = $query_text;
        $url = 'http://www.gdwz.net/open_cx/ser_wzdb2.aspx?';
        $o = '';
        //拼接参数
        foreach ($post_data as $k => $v) {
            $o .= "$k=" . $v . '&';
        }
        //去掉最后一个&
        $post_data = substr($o, 0, -1);
        //curl get方法
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url . $post_data);
        curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $output = curl_exec($ch);
        $contents = simplexml_load_string($output);
        var_dump($contents);
    }
    /*--------------↑↑↑违章接口完毕↑↑↑---------------*/
    
    //生成订单
    public function createOrder() {
        
        $order = M('order');
        $order_goods = M('order_goods');

        $user_id = $_REQUEST['user_id'];
        $goods_id = $_REQUEST['goods_id'];
        $order_type = $_REQUEST['order_type'];
        $color = $_REQUEST['color'];//汉字 or 16进制
        if (empty($user_id) || empty($goods_id) || empty($color)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }

        if($order_type == 1) {             //查询秒杀订单库存
            $redis = A('Redis');
            $redisRs = $redis -> checkSeckillGoodsStock($goods_id);
            if(!$redisRs) {
                $this ->ajaxReturn(array("status" => 4,"result_msg" => "商品已售空"));
            }
        } else if(empty ($order_type) || $order_type == 2) {  //查询普通、一口价订单库存
            if(empty($order_type)) {
                $order_type = 0;
            }
            $redis = A('Redis');
            $redisRs = $redis -> checkGoodsStock($goods_id);
            if(!$redisRs) {
                $this ->ajaxReturn(array("status" => 4,"result_msg" => "商品已售空"));
            }
        }
        //如果是秒杀商品，表order加个seckill_id字段
        $ress2 = $order->table('eb_seckill_set')->where(array('status'=>1,'seckill_type' => 0))->select();
        $seckill_id = $ress2[0]['id'];
        $ress3 = $order->table('eb_seckill_goods')->where(array('goods_id'=>$goods_id,'seckill_id'=>$seckill_id))->select();
        if ($ress3) {
            $data['seckill_id'] = $seckill_id;
        } else {
            $data['seckill_id'] = 0;
        }
        $res = $order->table(C('DB_PREFIX').'goods')->where(array('id'=>$goods_id,'status'=>'1','goods_type'=>0))->select();
        $res1 = $order->table(C('DB_PREFIX').'user')->where(array('id'=>$user_id,'status'=>'1'))->select();
        $phone = $res1['0']['phone'];
        $username = $res1['0']['nickname'];
        $name = $res['0']['name'];
        $price = $res['0']['price'];
        $deposit = $res['0']['deposit'];
        $thumb = $res['0']['thumb'];
        $guise = $color;
        $num = '1';
        $order_sn = getOrderTradeNo();
        //生成订单主表数组
        $data['order_sn'] = $order_sn;
        $data['user_id'] = $user_id;
        $data['add_time'] = time();
        $data['total_price'] = $deposit;
        $data['pay_type_id'] = '1';//默认支付宝
        $data['pay_status'] = '0';
        $data['phone'] = $phone;
        $data['username'] = $username;
        $data['order_type'] = $order_type;//订单类型 0-普通 1-秒杀 2-一口价
        $list = $order->add($data);

        //生成订单从表数组
        $data1['order_id'] = $list;
        $data1['goods_id'] = $goods_id;
        $data1['num'] = $num;
        $data1['order_sn'] = $order_sn;
        $data1['price'] = $deposit;
        $data1['user_id'] = $user_id;
        $data1['name'] = $name;
        $data1['thumb'] = $thumb;
        $data1['color'] = $guise;
        $data1['goods_price'] = $price;
        $list1 = $order_goods->add($data1);
        if (!$list1) {
            $code = array('status_code'=>'3','result_msg'=>'生成订单失败');
            echo arr_json($code);
            exit;
        }
        //生成个人消息中心
        M('sys_msg')->add(array('title'=>'商品消息','content'=>'您的订单已生成','status'=>'1','create_time'=>date('Y-m-d H:i:s',$data['add_time']),'type'=>'1','user_id'=>$user_id));
        $ress = $order_goods->where(array('id'=>$list1))->select();
        $code = array('status_code'=>'1','order_goods'=>$ress);
        echo arr_json($code);
    }
    
     //生成支付详细
    public function createPayDetail() {
        $pay_detail = M('pay_detail');
        $order = M('order');
        //$user_id = '2374';
        $user_id = $_REQUEST['user_id'];
        //$order_sn = '201703071417117008';
        $order_sn = $_REQUEST['order_sn'];
        //$pay_way = '1';
        $pay_way = $_REQUEST['pay_way'];
        if (empty($user_id) || empty($order_sn) || empty($pay_way)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $order->pay_type_id = $pay_way;
        $order->where(array('order_sn'=>$order_sn))->save();
        $res = $pay_detail->table(C('DB_PREFIX').'order')->where(array('order_sn'=>$order_sn,'user_id'=>$user_id))->select();
        //订单类型 0-普通订单 1-秒杀订单（更新销售数量sale_num） 2-一口价订单（更新库存量inventory）
        $order_type = $res['0']['order_type'];
        if (!$res) {
            $code = array('status_code'=>'3','result_msg'=>'该用户不存在此订单');
            echo arr_json($code);
            exit;
        }
//        $res1 = $pay_detail->where(array('order_sn'=>$order_sn))->select();
//        if (!$res1) {
//            //生成支付订单数组
//            $data['order_id'] = $res['0']['id'];
//            $data['pay_type'] = $pay_way;
//            $data['create_time'] = time();
//            $data['status'] = 0;
//            $data['order_sn'] = $order_sn;
//            $list = $pay_detail->add($data);
//        } 
        $res2 = $pay_detail->table(C('DB_PREFIX').'order_goods')->where(array('order_sn'=>$order_sn))->select();
        $name = $res2['0']['name'];
        $price = $res2['0']['price'];
        if ($pay_way == '1') {//支付宝
            $url = 'http://www.qhcarmall.com/M/car/Pay/alipay/index.php?';
            $post_data = array();
            $post_data['out_trade_no'] = $order_sn;
            $post_data['subject'] = $name;
            $post_data['total_amount'] = $price;
            $post_data['body'] = $name;
            $o = '';
            foreach ($post_data as $k=>$v) {
                $o .= $k.'='.$v.'&';
            }
            $post_data = substr($o,0,-1);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
            //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果需要将结果直接返回到变量里，那加上这句。
            $result = curl_exec($ch);
        } else if ($pay_way == '2') {
            echo '微信支付';
        } else {
            echo '其他支付方式暂未开通';
        }
    }
    
     //对未支付订单发起支付
    public function nonPayOrder() {
        $pay_detail = M('pay_detail');
        //$user_id = '2379';
        $user_id = $_REQUEST['user_id'];
        //$order_sn = '201702271951389411';
        $order_sn = $_REQUEST['order_sn'];
        //$pay_way = '1';
        $pay_way = $_REQUEST['pay_way'];
        if (empty($user_id) || empty($order_sn) || empty($pay_way)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $res = $pay_detail->table(C('DB_PREFIX').'order')->where(array('user_id'=>$user_id,'order_sn'=>$order_sn))->select();  
        //订单类型 0-普通订单 1-秒杀订单（更新销售数量sale_num） 2-一口价订单（更新库存量inventory）
        $order_type = $res['0']['order_type'];  
        $pay_status = $res['0']['pay_status'];
        if ($pay_status == '1') {
            $code = array('status_code'=>'3','result_msg'=>'该订单已支付');
            echo arr_json($code);
            exit;
        }   
        $res1 = $pay_detail->table(C('DB_PREFIX').'order_goods')->where(array('user_id'=>$user_id,'order_sn'=>$order_sn))->select();
        if (!$res || !$res1) {
            $code = array('status_code'=>'4','result_msg'=>'此用户没有该订单');
            echo arr_json($code);
            exit;
        }
        $out_trade_no = $order_sn;
        $subject = $res1['0']['name'];
        $total_amount = $res1['0']['price'];
        //$subject = '测试车网通';
        //$total_amount = '0.01';
        if ($pay_way == '1') {//支付宝支付
            $url = 'http://www.qhcarmall.com/M/car/Pay/alipay/index.php?';
            $post_data = array();
            $post_data['out_trade_no'] = $out_trade_no;
            $post_data['subject'] = $subject;
            $post_data['total_amount'] = $total_amount;
            $post_data['body'] = $subject;
            $o = '';
            foreach ($post_data as $k=>$v) {
                $o .= $k.'='.$v.'&';
            }
            $post_data = substr($o,0,-1);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
            //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果需要将结果直接返回到变量里，那加上这句。
            $result = curl_exec($ch);

        } else if ($pay_way == '2') {
            echo '微信支付';
        } else {
            echo '其它支付方式暂未开通';
        }
    }
    
    //图片上传
    public function upload() {
        //print_r($_FILES);exit;
        $upload = new \Think\Upload();
        //$upload->maxSize = 3145728;
        $upload->exts = array('jpg', 'jpeg', 'gif', 'png');
        $upload->saveName = array('uniqid','');
        $upload -> rootPath = './../uploads/';  //修改上传的更目录
        $upload->savePath = './image/';
        $info = $upload->upload();
        //$this ->ajaxReturn(array(info => $info));
        return $info;
    }
	
    //资讯列表接口
    public function articleList() {
        $res = M('article')->field('id,thumb,time,click,title,content')->where(array('status'=>1))->order('id desc')->limit(10)->select();
        foreach ($res as $k=>$v) {
            $res[$k]['time'] = date('Y-m-d',$v['time']);
            $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
        }
        $code = array('status_code'=>'1','result_msg'=>$res);
        echo arr_json($code);
    }
    
    //资讯更多接口
    public function articleMore() {
        $load_times = $_REQUEST['load_times'];
        if (empty($load_times)) {
            $load_times = 0;
        }
        $res = M('article')->field('id,thumb,time,click,title,content')->where(array('status'=>1))->order('time desc')->limit(10*$load_times,10)->select();
        if (!$res) {
            $code = array('status_code'=>'3','result_msg'=>'暂无更多资讯消息');
            echo arr_json($code);
            exit;
        }
        foreach ($res as $k=>$v) {
            $res[$k]['time'] = date('Y-m-d',$v['time']);
            $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
        }
        $code = array('status_code'=>'1','result_msg'=>$res);
        echo arr_json($code);
    }
    
    //资讯详情接口
    public function articleDetail() {
        $article_id = $_REQUEST['article_id'];
        $user_id = $_REQUEST['user_id'];
        if (empty($article_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        
        //微信分享
        $url = $_REQUEST['url'];
        $pay_config = D('PayConfig');
        $wx_config_data = $pay_config -> getOneList(array(pay_status => 0,use_status => 1), 'app_id,mch_id,key,app_secret');
        if(empty($wx_config_data)) {
            $jsSdk = new PublicClass1_0_0\WechatClass();
        } else {
            $jsSdk = new PublicClass1_0_0\WechatClass($wx_config_data['app_id'],$wx_config_data['mch_id'],$wx_config_data['key'],$wx_config_data['app_secret']);
        }

        $wx = $jsSdk ->getShareConfig($url);
        //该资讯的点击量+1
        M('article')->where(array('id'=>$article_id))->setInc('click',1);
        $res = M('article')->field('id,thumb,time,title,content,author,like_sum,description')->where(array('id'=>$article_id,'status'=>1))->select();
        foreach ($res as $k=>$v) {
            $res[$k]['time'] = date('Y-m-d',$v['time']);
            $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
            if (!empty($user_id)) {
                $ress = M('article_like')->where(array('article_id'=>$article_id,'user_id'=>$user_id))->select();
                if ($ress) {
                    $res[$k]['is_like'] = 1;
                } else {
                    $res[$k]['is_like'] = 0;
                }
            } else {
                unset($res[$k]['is_like']);
            }
        }
        $code = array('status_code'=>'1','result_msg'=>$res);
        $code['wx'] = $wx;
        echo arr_json($code);
    }

    //相关资讯接口
    public function relatedArticle() {
        //相关资讯逻辑：同类->id差值最小->时间排序6.2
        $article_id = $_REQUEST['article_id'];    
        if (empty($article_id)) {
            $code = array('status_code'=>2,'result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $res1 = M('article')->field('type')->where(array('id'=>$article_id))->select();
        $res = M('article')->field('id,thumb,click,time,title,type,author')->where(array('type'=>$res1[0]['type'],'status'=>1,'id'=>array('neq',$article_id)))->limit(3)->select();
        foreach ($res as $k=>$v) {
            $res[$k]['abs'] = abs($res[$k]['id'] - $article_id);
            $res[$k]['time'] = date('Y-m-d',$v['time']);
            $res[$k]['thumb'] = C('HTTP_HEAD').$v['thumb'];
            $abs[$k] = $res[$k]['abs'];
            $time[$k] = $res[$k]['time'];
        }        
        //对多维数组排序,order好像满足不了需求
        array_multisort($abs,SORT_ASC,$time,SORT_DESC,$res);
        $code = array('status_code'=>1,'result_msg'=>$res);
        echo arr_json($code);
    }

    //资讯点赞接口
    public function articleLike() {
        $user_id = $_REQUEST['user_id'];
        $article_id = $_REQUEST['article_id'];
        if (empty($user_id) || empty($article_id)) {
            $code = array('status_code'=>'2','result_msg'=>'缺少参数');
            echo arr_json($code);
            exit;
        }
        $res = M('article_like')->where(array('user_id'=>$user_id,'article_id'=>$article_id))->select();
        if ($res) {
            $code = array('status_code'=>'3','result_msg'=>'该资讯你已赞');
            echo arr_json($code);
            exit;
        }
        $list = M('article_like')->add(array('article_id'=>$article_id,'user_id'=>$user_id,'time'=>time()));
        $res1 = M('article_like')->where(array('article_id'=>$article_id))->select();
        $like_sum = count($res1);
        //更新点赞数
        M('article')->where(array('id'=>$article_id))->save(array('like_sum'=>$like_sum));
        $res2 = M('article')->where(array('id'=>$article_id))->select();
        $code = array('status_code'=>'1','result_msg'=>$res2);
        echo arr_json($code);
    }
    
    public function showRedis() {
        
        //查看所有key对应的value                                        
        $redis = new \Redis();     
        $host_name = exec("hostname");  
        $inner_ip = gethostbyname($host_name);//获取内网ip
        $redis->connect($inner_ip,'6379');
        $arr_keys = $redis->keys('*');
        $new_arr = array();
        foreach ($arr_keys as $k=>$v) {
            $k_v = $redis->get($v);
            $new_arr[$v] = $k_v;
        }
        ksort($new_arr,SORT_NATURAL);//第二个参数自然顺序排序
        p($new_arr);        
        
    }
    
    //订单详情（区分车和车品）
    public function orderDetail() {
        
        $goods_id = I('post.goods_id');        
        $user_id = I('post.user_id');    
        
        if (empty($goods_id) || empty($user_id)) {
            exit(arr_json(array('status_code'=>2,'result_msg'=>'缺少参数')));
        }
                
        $res1 = M('goods')->where(array('id'=>$goods_id,'status'=>1))->find();
        
        if ($res1['goods_type'] == 1) {//车品
            
            $goods_res = M('goods')->field('name,guise,sale_price,thumb')->where(array('id'=>$goods_id,'status'=>1))->find();             
            $address_res = M('user_address')->field('linkman,phone,province,city,zone,address')->where(array('user_id'=>$user_id,'status'=>1))->find();
            
            if (!$address_res) {
                exit(arr_json(array('status_code'=>3,'result_msg'=>'暂无收货地址，请先添加！')));
            }
            
            $res = $address_res + $goods_res;            
            $res['thumb'] = C('HTTP_HEAD').$res['thumb'];  
            
        } else {
            
            $goods_res = M('goods')->field('name,guise,deposit,thumb')->where(array('id'=>$goods_id,'status'=>1))->find();            
            $user_res = M('user')->field('username,phone')->where(array('id'=>$user_id,'status'=>1))->find();
            
            $res = $user_res + $goods_res;
            $res['thumb'] = C('HTTP_HEAD').$res['thumb'];
            $arr_guise = explode('|',$res['guise']);
            $res['guise'] = $arr_guise[0];
            
            $color_res = M('goods_color')->where(array('name'=>array('in',$res['guise'])))->select();
            
            $new_array = array();
            foreach ($color_res as $k=>$v) {
                $new_array[$v['name']] = $v['code'];
            }
            $res['colors'] = $new_array;
        } 
        
        exit(arr_json(array('status_code'=>1,'result_msg'=>$res)));
        
    }
    
    //接口不存在
    public function _empty() {
        echo '<center><span style="color:red;font-size:25px">请求的接口不存在,请输入正确接口地址！！！</span></center>';
    }
    
}
